// 关键词处理器 - 处理问卷中的动态关键词替换
import { storage } from '@/util/storage.js'

/**
 * 关键词处理器类
 */
class KeywordHandler {
    /**
     * 获取日期关键词
     * @param {string} type - 日期类型
     * @returns {string} 格式化的日期字符串
     */
    static getDate(type) {
        const date = new Date();
        switch (type) {
            case "tomorrow":
                date.setDate(date.getDate() + 1);
                break;
            case "yesterday":
                date.setDate(date.getDate() - 1);
                break;
            case "random":
                const num = Math.floor(Math.random() * 18) + 2; // 生成 2 到 19 之间的数
                const offset = Math.random() < 0.5 ? -num : num; // 随机决定正负
                date.setDate(date.getDate() + offset);
                break;
            case "today":
                date.setDate(date.getDate());
                break;
            default:
                break;
        }
        return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
    }

    /**
     * 获取位置关键词
     * @param {string} type - 位置类型
     * @returns {string} 位置字符串
     */
    static getLocation(type) {
        switch (type) {
            case "random-1":
                return "北京市";
            case "random-2":
                return "上海市";
            case "random-3":
                return "深圳市";
            case "user":
                return storage.get('user_location')?.city || "获取用户位置失败";
            default:
                return "";
        }
    }

    /**
     * 获取营养相关关键词
     * @param {string} type - 营养类型
     * @returns {string} 营养相关字符串
     */
    static getNutrition(type) {
        const userinfo = storage.get('user_info');
        if (!userinfo) {
            return "";
        }
        switch (type) {
            case "force-1":
                return userinfo.gender === 0 ? "优势手握力<28kg" : "优势手握力<18kg";
            case "force-2":
                return userinfo.gender === 0 ? "优势手握力≥28kg" : "优势手握力≥18kg";
            default:
                return "";
        }
    }
}

// 关键词映射表
const keywords = {
    "date-tomorrow": KeywordHandler.getDate("tomorrow"),
    "date-yesterday": KeywordHandler.getDate("yesterday"),
    "date-today": KeywordHandler.getDate("today"),
    "date-random": KeywordHandler.getDate("random"),
    "location-random-1": KeywordHandler.getLocation("random-1"),
    "location-random-2": KeywordHandler.getLocation("random-2"),
    "location-random-3": KeywordHandler.getLocation("random-3"),
    "location-user": KeywordHandler.getLocation("user"),
    "nutrition-force-1": KeywordHandler.getNutrition("force-1"),
    "nutrition-force-2": KeywordHandler.getNutrition("force-2"),
}

/**
 * 获取关键词值
 * @param {string} key - 关键词键
 * @returns {string} 关键词值
 */
export const getKeyword = (key) => {
    if (!keywords[key]) {
        console.error(`未找到关键词: ${key}`);
        return '';
    }
    return keywords[key];
}
